﻿using System;
using System.Data;
using System.Linq;
using System.Web.Mvc;
using GioHangViet.Areas.Admin.Models;
using GioHangViet.Models;

namespace GioHangViet.Areas.Admin.Controllers
{ 
    public class AuthorController : Controller
    {
        private readonly GoLungTungModel _db = new GoLungTungModel();
        private readonly ProcessAlias _pa = new ProcessAlias();

        //
        // GET: /Admin/Author/

        public ViewResult Index(int id)
        {
            if (id < 1)
            {
                id = 1;
            }
            ViewBag.CurrentPage = id;
            ViewBag.countRecord = _db.Authors.Count();
            var pageSize = 10D;
            if (Double.TryParse(Resources.Constant.PageSize, out pageSize))
            {
                ViewBag.PageSize = pageSize;
            }
            else
            {
                ViewBag.PageSize = pageSize;
            }
            var model =
                _db.Authors.ToList().Skip((id - 1) * Convert.ToInt32(Resources.Constant.PageSize)).Take(
                    Convert.ToInt32(Resources.Constant.PageSize));
            return View(model);
        }

        //
        // GET: /Admin/Author/Create

        public ActionResult Create()
        {
            var alias = new Author {Alias = "author"};
            return View(alias);
        } 

        //
        // POST: /Admin/Author/Create

        [HttpPost]
        public ActionResult Create(Author author)
        {
            if (ModelState.IsValid)
            {
                var name = author.Authorname;
                var alias = _pa.CreateAlias(name, "Authors", "Alias");
                author = new Author {Alias = alias, Authorname = name};
                _db.Authors.Add(author);
                _db.SaveChanges();
                return RedirectToAction("Index", new{id = 1});  
            }

            return View(author);
        }

        //
        // GET: /Admin/Category/Edit/5

        public ActionResult Edit(string id)
        {
            Author author = _db.Authors.Find(id);
            return View(author);
        }

        //
        // POST: /Admin/Category/Edit/5

        [HttpPost]
        public ActionResult Edit(Author author)
        {
            if (ModelState.IsValid)
            {
                _db.Entry(author).State = EntityState.Modified;
                _db.SaveChanges();
                return RedirectToAction("Index");
            }
            return View(author);
        }

        //
        // GET: /Admin/Author/Delete/5
 
        public ActionResult Delete(string id)
        {
            var author = _db.Authors.Find(id);
            if (author != null)
            {
                _db.Authors.Remove(author);
                _db.SaveChanges();
                return RedirectToAction("Index", new {id = 1});
            }
            return View("Error");
        }

        protected override void Dispose(bool disposing)
        {
            _db.Dispose();
            base.Dispose(disposing);
        }
    }
}